using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WebControls;
using EntityLayer;
using BusinessLayer;

public partial class Admin_users : SFPage
{
    entUsers insEntUsers = new entUsers();
    busUsers insBusUsers = new busUsers();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected bool sfgUsers_NeedsDataSource()
    {
        busUsers insBusUsers = new busUsers();
        sfgUsers.SetData( insBusUsers.SelectUsers());
        return false;
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        this.Id = 0;
        this.OpenEditPanel();
    }
    protected void sfgUsers_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "EditData")
        {
            this.Id = Convert.ToInt32(e.CommandArgument);
            this.OpenEditPanel();
        }
        else if (e.CommandName == "DeleteData")
        {
            insEntUsers.UserId= Convert.ToInt32(e.CommandArgument);
            insBusUsers.DeleteUsersByUserId(insEntUsers);
            if (insBusUsers.HasErrors)
            {
                MessageBoxShow(insBusUsers);
                return;
            }
            sfgUsers.LoadGridData();
            this.CloseEditPanel();
        }
    }
    public override void OpenEditPanel()
    {
        this.Clear(this.Master.FindControl("ContentPlaceHolderEdit"));
        base.OpenEditPanel();
        FillEditPanel();
    }
    private void FillEditPanel()
    {
        if (this.Id == 0)
        {
            txtUserName.Text = "";
            txtUserPassword.Text = "";
        }
        else
        {
            entUsers insEntUsers = new entUsers();
            busUsers insBusUsers = new busUsers();
            insEntUsers.UserId = this.Id;
            insBusUsers.SelectUsersByUserId(insEntUsers);
            txtUserName.Text = insEntUsers.UserName;
            txtUserPassword.Text = "";
        }
    }

    public override void Save()
    {
        this.Validate();
        if (this.HasErrors)
        {
            MessageBoxShow(this);
            return;
        }
        insEntUsers.UserName = txtUserName.Text;
        insEntUsers.UserPassword = txtUserPassword.Text;
        base.Save();
    }
    public override void SaveNew()
    {
        insBusUsers.InsertUsers(insEntUsers);
        if (insBusUsers.HasErrors)
        {
            MessageBoxShow(insBusUsers);
            return;
        }
        base.SaveNew();
        sfgUsers.LoadGridData();
        
    }
    public override void SaveExisting()
    {
        insEntUsers.UserId = this.Id;
        insBusUsers.UpdateUsersByUserId(insEntUsers);
        if (insBusUsers.HasErrors)
        {
            MessageBoxShow(insBusUsers);
            return;
        }
        base.SaveNew();
        sfgUsers.LoadGridData();

    }

}
